package com.iwomy.envpro.platform.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.log.annotation.SysLog;
import com.iwomy.envpro.platform.entity.AirMonitorEquipmentEntity;
import com.iwomy.envpro.platform.entity.BasicEnterpriseTypeEntity;
import com.iwomy.envpro.platform.entity.BasicEquipmentModelEntity;
import com.iwomy.envpro.platform.exception.PlatformException;
import com.iwomy.envpro.platform.service.AirMonitorEquipmentService;
import com.iwomy.envpro.platform.service.BasicEquipmentModelService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.envpro.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备型号
 *
 * @author pig
 * @date 2023-09-23 15:49:20
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicEquipmentModel" )
@Tag(description = "basicEquipmentModel" , name = "设备型号管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicEquipmentModelController {

    private final  BasicEquipmentModelService basicEquipmentModelService;

	private final AirMonitorEquipmentService airMonitorEquipmentService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param basicEquipmentModel 设备型号
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('platform_basicEquipmentModel_view')" )
    public R getBasicEquipmentModelPage(@ParameterObject Page page,@ParameterObject BasicEquipmentModelEntity basicEquipmentModel) {
        LambdaQueryWrapper<BasicEquipmentModelEntity> wrapper = Wrappers.lambdaQuery(basicEquipmentModel);
        return R.ok(basicEquipmentModelService.page(page, wrapper));
    }


    /**
     * 通过id查询设备型号
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('platform_basicEquipmentModel_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(basicEquipmentModelService.getById(id));
    }

    /**
     * 新增设备型号
     * @param basicEquipmentModel 设备型号
     * @return R
     */
    @Operation(summary = "新增设备型号" , description = "新增设备型号" )
    @SysLog("新增设备型号" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform_basicEquipmentModel_add')" )
    public R save(@RequestBody BasicEquipmentModelEntity basicEquipmentModel) {
		BasicEquipmentModelEntity wrapperEntity = new BasicEquipmentModelEntity();
		wrapperEntity.setName(basicEquipmentModel.getName());
		LambdaQueryWrapper<BasicEquipmentModelEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicEquipmentModelEntity one = basicEquipmentModelService.getOne(wrapper);
		if(ObjectUtil.isNotNull(one)){
			throw new PlatformException("设备型号已存在");
		}
        return R.ok(basicEquipmentModelService.save(basicEquipmentModel));
    }

    /**
     * 修改设备型号
     * @param basicEquipmentModel 设备型号
     * @return R
     */
    @Operation(summary = "修改设备型号" , description = "修改设备型号" )
    @SysLog("修改设备型号" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform_basicEquipmentModel_edit')" )
    public R updateById(@RequestBody BasicEquipmentModelEntity basicEquipmentModel) {


		LambdaQueryWrapper<AirMonitorEquipmentEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(AirMonitorEquipmentEntity::getEquipmentModelId, basicEquipmentModel.getId());
		long count = airMonitorEquipmentService.count(queryWrapper);
		if (count > 0) {
			return R.failed("设备型号关联了设备，不允许编辑");
		}

		BasicEquipmentModelEntity wrapperEntity = new BasicEquipmentModelEntity();
		wrapperEntity.setName(basicEquipmentModel.getName());
		LambdaQueryWrapper<BasicEquipmentModelEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicEquipmentModelEntity one = basicEquipmentModelService.getOne(wrapper);
		if(ObjectUtil.isNotNull(one)&&!one.getId().equals(basicEquipmentModel.getId())){
			throw new PlatformException("设备型号已存在");
		}
        return R.ok(basicEquipmentModelService.updateById(basicEquipmentModel));
    }

    /**
     * 通过id删除设备型号
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除设备型号" , description = "通过id删除设备型号" )
    @SysLog("通过id删除设备型号" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform_basicEquipmentModel_del')" )
    public R removeById(@RequestBody Long[] ids) {

		long count = airMonitorEquipmentService.count(Wrappers.<AirMonitorEquipmentEntity>lambdaQuery().in(AirMonitorEquipmentEntity::getEquipmentModelId, ids));

		if (count > 0)
			return R.failed("设备型号关联了设备，不允许删除");

        return R.ok(basicEquipmentModelService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param basicEquipmentModel 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('platform_basicEquipmentModel_export')" )
    public List<BasicEquipmentModelEntity> export(BasicEquipmentModelEntity basicEquipmentModel) {
        return basicEquipmentModelService.list(Wrappers.query(basicEquipmentModel));
    }
    /**
     * @param basicEquipmentModel 查询条件
     */
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('platform_basicEquipmentModel_export')" )
    public R list(BasicEquipmentModelEntity basicEquipmentModel) {
        return R.ok(basicEquipmentModelService.list(Wrappers.query(basicEquipmentModel)));
    }
}